package view;

import java.io.IOException;
import java.net.UnknownHostException;

import model.roomManagement.Implementation.Server;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;

import controller.ServerController;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Text;

public class ServerView implements ViewObserver {

	protected Shell shlGms;
	private Display display;
	private Button btn_enter;
	private Button btn_close;
	private Label lbl_s;
	private Label lbl_uNum;
	private Label lbl_userNum;
	private Label lbl_rNum;
	private Label lbl_roomNum;

	private Label lbl_warning;

	private Server server;
	private ServerController sc;
	private Label lbl_pic;
	
	private Button btn_cancel;
	private Button btn_exit;
	private Label lbl_exit;
	private Text text_serverIP;
	
	public static void main(String[] args) {
		try {
			ServerView window = new ServerView();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ServerView() {

	}

	public ServerView(Server server, ServerController sc) {
		this.server = server;
		this.sc = sc;
		server.addViewObserver(this);
	}

	/**
	 * Open the window.
	 */
	public void open() {
		display = Display.getDefault();
		createContents();
		shlGms.open();
		shlGms.layout();
		while (!shlGms.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlGms = new Shell(SWT.MIN | SWT.CLOSE);
		shlGms.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				e.doit = false;
				if(btn_close.isVisible()){

					lbl_rNum.setVisible(false);
					lbl_roomNum.setVisible(false);
					lbl_s.setVisible(false);
					text_serverIP.setVisible(false);
					lbl_uNum.setVisible(false);
					lbl_userNum.setVisible(false);
					btn_close.setVisible(false);

					btn_cancel.setVisible(true);
					btn_exit.setVisible(true);
					lbl_exit.setVisible(true);
				
				}
				else if(btn_enter.isVisible()){
					System.exit(0);
				}
			}
		});
		shlGms.setImage(SWTResourceManager.getImage(ServerView.class, "/img/server_148.png"));
		shlGms.setSize(280, 505);
		shlGms.setText("GMS服务器");

		Composite composite = new Composite(shlGms, SWT.NONE);
		composite.setBounds(0, 0, 275, 480);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBackgroundImage(SWTResourceManager.getImage(
				ServerView.class, "/img/back_275_480_server.jpg"));

		btn_enter = new Button(composite, SWT.NONE);
		btn_enter.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		btn_enter.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				try {
					sc.open();
					lbl_rNum.setVisible(true);
					lbl_roomNum.setVisible(true);
					lbl_s.setVisible(true);
					text_serverIP.setVisible(true);
					lbl_uNum.setVisible(true);
					lbl_userNum.setVisible(true);
					btn_close.setVisible(true);

					btn_enter.setVisible(false);
					lbl_pic.setVisible(false);
					text_serverIP.setText(Server.serverIp);// ��ʾ������IP��ַ
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		btn_enter.setBounds(96, 334, 80, 27);
		btn_enter.setText("\u5F00\u542F\u670D\u52A1\u5668");

		lbl_s = new Label(composite, SWT.NONE);
		lbl_s.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		lbl_s.setBounds(22, 170, 70, 17);
		lbl_s.setText("\u670D\u52A1\u5668IP\uFF1A");
		lbl_s.setVisible(false);
		
		text_serverIP = new Text(composite, SWT.READ_ONLY);
		text_serverIP.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		text_serverIP.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_serverIP.setBounds(96, 170, 148, 17);
		text_serverIP.setVisible(false);

		btn_close = new Button(composite, SWT.NONE);
		btn_close.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		btn_close.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lbl_rNum.setVisible(false);
				lbl_roomNum.setVisible(false);
				lbl_s.setVisible(false);
				text_serverIP.setVisible(false);
				lbl_uNum.setVisible(false);
				lbl_userNum.setVisible(false);
				btn_close.setVisible(false);

				btn_cancel.setVisible(true);
				btn_exit.setVisible(true);
				lbl_exit.setVisible(true);
			}
		});
		btn_close.setText("\u5173\u95ED\u670D\u52A1\u5668");
		btn_close.setBounds(96, 334, 80, 27);
		btn_close.setVisible(false);

		lbl_uNum = new Label(composite, SWT.NONE);
		lbl_uNum.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		lbl_uNum.setBounds(22, 215, 70, 17);
		lbl_uNum.setText("\u5728\u7EBF\u4EBA\u6570\uFF1A");
		lbl_uNum.setVisible(false);

		lbl_userNum = new Label(composite, SWT.NONE);
		lbl_userNum.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		lbl_userNum.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lbl_userNum.setBounds(96, 215, 148, 17);
		lbl_userNum.setVisible(false);
		lbl_userNum.setText("" + server.getClientCount());

		lbl_rNum = new Label(composite, SWT.NONE);
		lbl_rNum.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		lbl_rNum.setBounds(22, 260, 70, 17);
		lbl_rNum.setText("\u5728\u7EBF\u4F1A\u8BAE\u5BA4\uFF1A");
		lbl_rNum.setVisible(false);

		lbl_roomNum = new Label(composite, SWT.NONE);
		lbl_roomNum.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		lbl_roomNum.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lbl_roomNum.setBounds(96, 260, 148, 17);
		lbl_roomNum.setVisible(false);
		lbl_roomNum.setText("" + server.getRoomCount());

		lbl_warning = new Label(composite, SWT.NONE);
		lbl_warning.setAlignment(SWT.CENTER);
		lbl_warning.setBounds(60, 370, 150, 17);

		lbl_pic = new Label(composite, SWT.NONE);
		lbl_pic.setImage(SWTResourceManager.getImage(ServerView.class,
				"/img/server_148.png"));
		lbl_pic.setAlignment(SWT.CENTER);
		lbl_pic.setBounds(60, 140, 150, 150);
		
		btn_cancel = new Button(composite, SWT.NONE);
		btn_cancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btn_cancel.setVisible(false);
				btn_exit.setVisible(false);
				lbl_exit.setVisible(false);
				
				btn_close.setVisible(true);
				lbl_s.setVisible(true);
				text_serverIP.setVisible(true);
				lbl_uNum.setVisible(true);
				lbl_userNum.setVisible(true);
				lbl_rNum.setVisible(true);
				lbl_roomNum.setVisible(true);
			}
		});
		btn_cancel.setBounds(60, 270, 60, 27);
		btn_cancel.setText("\u53D6\u6D88");
		btn_cancel.setVisible(false);
		
		btn_exit = new Button(composite, SWT.NONE);
		btn_exit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					sc.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btn_exit.setBounds(150, 270, 60, 27);
		btn_exit.setText("\u786E\u8BA4");
		btn_exit.setVisible(false);
		
		lbl_exit = new Label(composite, SWT.WRAP);
		lbl_exit.setAlignment(SWT.CENTER);
		lbl_exit.setBounds(24, 200, 230, 34);
		lbl_exit.setText("\u5173\u95ED\u670D\u52A1\u5668\u4F1A\u4F7F\u5DF2\u7ECF\u8FDE\u4E0A\u7684\u7528\u6237\u65AD\u5F00\u8FDE\u63A5\uFF0C\u786E\u8BA4\u5173\u95ED\uFF1F");
		lbl_exit.setVisible(false);
	}

	@Override
	public void updata() {
		// TODO Auto-generated method stub
		final int userNum = server.getClientCount();// ���ߵ�����
		final int roomNum = server.getRoomCount();// ���ߵķ�����
		display.asyncExec(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				lbl_userNum.setText(userNum + "");
				lbl_roomNum.setText(roomNum + "");
			}
		});
		
	}

	// ��̨��ʾ��ʾ**************************************************************************
	class BackgroundThread extends Thread {
		private String str;

		public BackgroundThread(String str) {
			this.str = str;
		}

		public void run() {
			display.asyncExec(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					lbl_warning.setText(str);
					lbl_warning.setVisible(true);
				}
			});
			try {
				sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			display.asyncExec(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					lbl_warning.setVisible(false);
				}
			});
		}
	}
}
